// 处理时间的函数，先将字符串转化为时间类型的数据，再进行调整，调整后重新转化为字符串进行缩减
function changeTime(date) {
    var dateee = new Date(date).toJSON();
    return new Date(+new Date(dateee) + 8 * 3600 * 1000).toISOString().replace(/T/g, ' ').replace(/\.[\d]{3}Z/, '')
}
window.onload = function () {
    changePattern();
    //获取动态ID
    var dynamicid = getSession("dynamicid");
    //获取登录用户信息
    var userData = getSession("data");
    var title = document.getElementById("title");
    var dynamicInfo = document.getElementById("dynamicInfo");
    var content = document.getElementById("content");
    var id = document.getElementById("id");
    var name = document.getElementById("name");
    var contentWrapper = document.getElementById("content-wrapper")
    var commentWrapper = document.getElementById("commentWrapper");
    //发送请求，展示动态详细内容
    ajax({
        type: "get",
        url: "http://www.rushmc.top/api/dynamicState/search/",
        data: {
            key: dynamicid
        },
        success: function (response) {
            var data = response.data[0];
            title.innerHTML = data.title;
            var time = changeTime(data.creationTime);
            dynamicInfo.innerHTML = data.username + '&nbsp' + '&nbsp' + '&nbsp' + '&nbsp' + time;
            content.innerHTML = data.content;
            id.innerHTML = data.username;
            if (data != null) {
                name.innerHTML = data.username;
                id.innerHTML = "ID:" + data.userId;
            }
            var S_wrapper = document.createElement("div");
            S_wrapper.id = "S_wrapper";
            var addComment = document.createElement("div");
            addComment.id = "addComment";
            addComment.contentEditable = "true";
            addComment.style.display = "none";
            var a = document.createElement("a");
            a.innerHTML = "评论";
            a.id = "add";
            var confirm = document.createElement("a");
            confirm.innerHTML = "确定";
            confirm.id = "confirm";
            confirm.style.display = "none";
            for (let i = 0; i < data.urls.length; i++) {
                var img = document.createElement("img");
                img.src = "http://www.rushmc.top/" + data.urls[i];
                S_wrapper.appendChild(img);
            }
            contentWrapper.appendChild(S_wrapper);
            contentWrapper.appendChild(a);
            contentWrapper.appendChild(addComment);
            contentWrapper.appendChild(confirm);
            //等待上面节点生成完毕再获取
            var addComment = document.getElementById("addComment")
            var add = document.getElementById("add");
            var confirm = document.getElementById("confirm");
            var addflag = -1;
            //点击评论弹出编辑框
            add.onclick = function () {
                if (!getSession("data")) {
                    add.style.marginLeft = "606px";
                    add.innerHTML = "游客没有评论功能";
                    return
                }
                addflag = -addflag;
                if (addflag == 1) {
                    add.innerHTML = "取消评论";
                    addComment.style.display = "block";
                    add.style.marginLeft = "675px"
                    confirm.style.display = "block";
                } else {
                    add.innerHTML = "评论";
                    addComment.style.display = "none";
                    confirm.style.display = "none";
                    addComment.innerHTML = "";
                    add.style.marginLeft = "700px"
                }
            }
            //点击确认发送评论请求
            confirm.onclick = function () {
                if (addComment.innerHTML == "" || addComment.innerHTML == "请输入内容") {
                    addComment.innerHTML = "请输入内容";
                    return;
                } else {
                    ajax({
                        type: "post",
                        url: "http://www.rushmc.top/api/comment/insert",
                        data: {
                            dynamicStateId: dynamicid,
                            replyerId: userData.id,
                            beReplyerId: data.userId,
                            commentsContent: addComment.innerHTML
                        },
                        success: function () {
                            add.innerHTML = "评论成功";
                            addComment.style.display = "none";
                            confirm.style.display = "none";
                            addComment.innerHTML = "";
                            add.style.marginLeft = "675px";
                            addflag = -1;
                            setTimeout(() => {
                                add.style.marginLeft = "700px";
                                add.innerHTML = "评论";
                            }, 1000);
                            commentWrapper.innerHTML = "";
                            showComment();
                        }
                    })
                }
            }

        }


    })
    // 评论区的展示函数
    function showComment() {
        ajax({
            type: "post",
            url: "http://www.rushmc.top/api/getReplyById",
            data: {
                dynamicId: dynamicid
            },
            success: function (response) {
                if (response.code == 1001) {
                    commentWrapper.innerHTML += "暂无评论";
                }
                var commentInfo = response.data;
                var ulArr = new Array;
                if (commentInfo) {
                    for (let i = 0; i < commentInfo.length; i++) {
                        //创造UL节点
                        let ul = document.createElement("ul");
                        let spanDiv = document.createElement("div");
                        let p = document.createElement("p");
                        let a = document.createElement("a");
                        let div = document.createElement("div");
                        let div_flag = -1;
                        let Reply_confirm = document.createElement("a");
                        let replyerPhoto = document.createElement("img");
                        let bereplyerPhoto = document.createElement("img");
                        // 为每个对话框设置一个属性，便于后续嵌套处理
                        ul.setAttribute("commentId", commentInfo[i].id);
                        Reply_confirm.innerHTML = "回复";
                        Reply_confirm.className = "reply";
                        // 增加回复者和被回复者的头像
                        if (commentInfo[i].replyerPhoto) {
                            replyerPhoto.src = "http://www.rushmc.top/" + commentInfo[i].replyerPhoto;
                        } else {
                            replyerPhoto.src = "../img/visitor.jpg"
                        }
                        if (commentInfo[i].beReplyerPhoto) {
                            bereplyerPhoto.src = "http://www.rushmc.top/" + commentInfo[i].beReplyerPhoto;
                        } else {
                            bereplyerPhoto.src = "../img/visitor.jpg"
                        }
                        if (commentInfo[i].replyId == 0) {
                            spanDiv.appendChild(replyerPhoto)
                            spanDiv.innerHTML += '"' + commentInfo[i].replyerName + '"' + "  评论:  "
                        } else {
                            spanDiv.appendChild(replyerPhoto)
                            spanDiv.innerHTML += '"' + commentInfo[i].replyerName + '"' + "  回复  "
                            spanDiv.appendChild(bereplyerPhoto);
                            spanDiv.innerHTML += '"' + commentInfo[i].beReplyerName + '"'
                        }
                        p.innerHTML = commentInfo[i].commentsContent;
                        a.innerHTML = "删除";
                        div.contentEditable = "true";
                        div.style.display = "none";
                        Reply_confirm.style.display = "none";
                        ul.appendChild(spanDiv);
                        ul.appendChild(a);
                        ul.appendChild(p);
                        ul.appendChild(div);
                        ul.appendChild(Reply_confirm);
                        //非本人评论无法删除
                        if (!getSession("data") || commentInfo[i].replyerId != userData.id) {
                            ul.removeChild(a);
                        }
                        a.onclick = function () {
                            if (confirm("是否删除该评论")) {
                                ajax({
                                    type: "post",
                                    url: "http://www.rushmc.top/api/comment/delete",
                                    data: {
                                        id: commentInfo[i].id
                                    },
                                    success: function () {
                                        a.innerHTML = "删除成功！"
                                        commentWrapper.innerHTML = "";
                                        showComment();
                                    }
                                })
                            }
                        }
                        // 点击文本信息回复
                        p.onclick = function (event) {
                            if (!getSession("data")) {
                                return
                            }
                            div_flag = -div_flag;
                            if (div_flag == 1) {
                                div.style.display = "block";
                                Reply_confirm.style.display = "block";
                            } else {
                                div.style.display = "none";
                                Reply_confirm.style.display = "none";
                            }
                        }
                        div.onkeydown=function(e){
                            if (e.keyCode==13){
                                console.log(e.keyCode)
                                var reg=/\w{45}/;
                            if (div.innerHTML == "" || (div.innerHTML.indexOf("请输入内容",0)!=-1)) {
                                div.innerHTML = "请输入内容";
                                return
                            }else if(reg.test(div.innerHTML)){
                                div.innerHTML += "最多为45个字符。";
                                return 
                            }else {
                                ajax({
                                    type: "post",
                                    url: "http://www.rushmc.top/api/comment/reply",
                                    data: {
                                        dynamicStateId: dynamicid,
                                        replyId: commentInfo[i].id,
                                        replyerId: userData.id,
                                        beReplyerId: commentInfo[i].replyerId,
                                        commentsContent: div.innerHTML
                                    },
                                    success: function () {
                                        div.style.display = "none";
                                        ul.removeChild(Reply_confirm);
                                        div_flag = -1;
                                        commentWrapper.innerHTML = "";
                                        showComment();
                                    }
                                })
                            }

                            }
                        }
                        // 增加回复的点击事件
                        Reply_confirm.onclick = function () {
                            var reg=/\w{45}/;
                            if (div.innerHTML == "" || (div.innerHTML.indexOf("请输入内容",0)!=-1)) {
                                div.innerHTML = "请输入内容";
                                return
                            }else if(reg.test(div.innerHTML)){
                                div.innerHTML += "最多为45个字符。";
                                return 
                            }else {
                                console.log(div.innerHTML.indexOf("dwdwdw",0))
                                ajax({
                                    type: "post",
                                    url: "http://www.rushmc.top/api/comment/reply",
                                    data: {
                                        dynamicStateId: dynamicid,
                                        replyId: commentInfo[i].id,
                                        replyerId: userData.id,
                                        beReplyerId: commentInfo[i].replyerId,
                                        commentsContent: div.innerHTML
                                    },
                                    success: function () {
                                        div.style.display = "none";
                                        ul.removeChild(Reply_confirm);
                                        div_flag = -1;
                                        commentWrapper.innerHTML = "";
                                        showComment();
                                    }
                                })
                            }

                        }
                        ulArr.push(ul);
                        if (commentInfo[i].replyId == 0) {
                            commentWrapper.appendChild(ul);
                        } else {
                            // 遍历所有对话框，通过commentId实现评论回复的嵌套
                            for (let x = 0; x < ulArr.length; x++) {
                                if (commentInfo[i].replyId == parseInt(ulArr[x].getAttribute("commentId"))) {
                                    ulArr[x].appendChild(ul);
                                } else {
                                    continue;
                                }
                            }
                        }
                    }
                }
            }
        }, "normal")
    }
    showComment()
    var up = document.getElementById("up");
    moveup(up);
    search();
}